package com.project.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.project.dao.DocumentDao.DocumentRowMapper;
import com.project.entity.Document;
import com.project.entity.Idea;
import com.project.entity.Ideacomment;
import com.project.entity.User;
import com.project.util.DBHelp;
import com.project.util.DateUtil;
import com.project.util.PKUtil;
import com.project.util.Page;
import com.project.util.RowMapper;

public class CommentServiceDao {
	DBHelp< Ideacomment> db=new DBHelp<Ideacomment>();
 	public void save(Ideacomment ic) {
		String sql="insert into t_ideacomment(id,cont,createtime,userid,ideaid) values(?,?,?,?,?)";
		db.executeSQL(sql,ic.getId(),ic.getCont(),ic.getCreatetime(),ic.getUserid(),ic.getIdeaid());
	}
	
	public List<Ideacomment> findCommentByIdeaid(String id) {
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT t_ideacomment.id,t_ideacomment.cont,t_ideacomment.createtime,t_ideacomment.userid,t_ideacomment.ideaid,t_user.id ,t_user.username AS username  FROM t_ideacomment ");
		sql.append("LEFT JOIN t_user ON t_user.id=t_ideacomment.userid  ");
		sql.append("WHERE t_ideacomment.ideaid= ? order by createtime desc");
		
		return db.executeQueryForList(sql.toString(), new IdeacommentRowMapper(), id);
	}

	private class IdeacommentRowMapper implements RowMapper<Ideacomment>{

		public Ideacomment mapRow(ResultSet rs) throws SQLException {
			
			Ideacomment ic=new Ideacomment();
			ic.setId(rs.getString("id"));
			ic.setCont(rs.getString("cont"));
			ic.setCreatetime(rs.getString("createtime"));
			ic.setIdeaid(rs.getString("ideaid"));
			ic.setUserid(rs.getString("userid"));
			User user=new User();
			user.setUsername(rs.getString("username"));
			user.setId(rs.getString("id"));
			
			ic.setUser(user);
			
			return ic;
		}
		
	
	}

	public void delCommentById(String id) {
		String sql="delete from t_ideacomment where id=?";
		db.executeSQL(sql, id);
	}

	public void delAllCommentById(String id) {
		
		String sql="delete from t_ideacomment where ideaid=?";
		db.executeSQL(sql, id);
	}
	public Long getCount() {
		String sql = "select count(*) from t_ideacomment";
		return (Long) db.executeQueryForFunction(sql);
	}

	/*public Page<Idea> findCommentByIdeaId(String id, Page<Idea> page) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_ideacomment.id,t_ideacomment.name,t_ideacomment.cont,t_ideacomment.createtime,userid,projectid,t_user.username AS 'username' ,t_user.password AS 'password' FROM t_document ");
		sql.append("left join t_user on t_user.id = t_ideacomment.userid ");
		sql.append("left join t_project on t_idea.id = t_ideacomment.ideaid ");
		sql.append("where ideaid = ? ORDER BY t_ideacomment.createtime DESC limit ?,? ");
		
		List<Ideacomment> list =  db.executeQueryForList(sql.toString(), new IdeacommentRowMapper(), id,page.getOffSet(),page.getPageSize());
		page.setResult(list);
		
		return page;
	}*/
	
	
}
